﻿package com.riatimes.net.socket
{
	import flash.events.ProgressEvent;
	import flash.net.Socket;
	import flash.utils.ByteArray;


    /**
     * Socket对象
     * @author Nick Wong
     */
    public class GameSocket extends Socket
    {
		
		private var myData:GameData;//socket数据处理对象
        public function GameSocket()
        {
			 myData = new GameData();
            this.addEventListener(ProgressEvent.SOCKET_DATA, getData);
        }
		/**
		 * 获取数据
		 * @param event
		 * 
		 */		
        private function getData(event:ProgressEvent) : void
        {
            var byteArray:ByteArray = new ByteArray();
            readBytes(byteArray, 0);
            myData.RevNetMessage(byteArray);
        }
		/**
		 *发送数据 
		 * @param cmd
		 * @param data
		 * 
		 */		
        public function _sendData(cmd:uint, data:String) : void
        {
			
            var byteArray:ByteArray = myData.Encapsulation(cmd, data);
			
            this.writeBytes(byteArray, 0, byteArray.length);
            this.flush();
        }
    }
}
